#!/usr/bin/env python

import MySQLdb
from sqlite3 import dbapi2 as sqlite
import os
from string import letters

def importAfis():
    mydb = MySQLdb.connect(host='localhost', user='root', db='medcron')
    cursormy = mydb.cursor()
    litedb = sqlite.connect('crud1.sqlite')
    cursorlite = litedb.cursor()
    for letra in letters[:26]:
        query = "select afi_nombre, afi_fdnac from afiliados where afi_nombre like '" + letra + "%' order by afi_nombre limit 0, 100"
        cursormy.execute(query)
        row = "@"
        while row:
            try:
                row = cursormy.fetchone()
                if row:
                    print "Inserting %s, %s" % (row[0], str(row[1]))
                    cursorlite.execute("insert into afiliados (nombre, fechanac) values ('%s', '%s')" % (row[0].decode('latin-1', 'replace'), str(row[1])))
                    litedb.commit()
            except:
                pass
            
if __name__ == '__main__':
    importAfis()